package com.gis3c.sys.dao.impl;

import com.gis3c.sys.dao.MenuDao;
import com.gis3c.sys.entry.po.App;
import com.gis3c.sys.entry.po.Menu;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.*;

/**
 * Created by hukekuan on 2017/12/22.
 */
@Repository
public class MenuDaoImpl extends SqlSessionDaoSupport implements MenuDao {
    @Resource(name="sqlSessionFactory_sys") private SqlSessionFactory sqlSessionFactory;

    @PostConstruct
    public void injectSessionFactory(){
        super.setSqlSessionFactory(sqlSessionFactory);
    }

    @Override
    public List<Menu> findMenuList(String appId) {
        return getSqlSession().selectList("MenuSql.findMenuList", appId);
    }

    @Override
    public List<Menu> findMenuListForRole(Set<String> roleIds, String appId) {
        Map<String,Object> params = new HashMap<>();
        params.put("roleIds",roleIds);
        params.put("appId",appId);

        return getSqlSession().selectList("MenuSql.findMenuListForRole",params);
    }

    @Override
    public Menu findMenuById(String menuId) {
        return getSqlSession().selectOne("MenuSql.findParentMenuById",menuId);
    }

    @Override
    public List<Menu> findMenuList(String appId, String parentMenuId) {
        return null;
    }

    @Override
    public List<Menu> findMenusByPage(String appId, String parentMenuId, int pageSize, int currentPage) {
        Map<String,Object> params = new HashMap<>();
        params.put("appId",appId);
        params.put("parentMenuId",parentMenuId);
        params.put("pageSize",pageSize);
        params.put("currentPage",currentPage);
        return getSqlSession().selectList("MenuSql.findMenusByPage",params);
    }

    @Override
    public Integer findMenuCount(String appId, String parentMenuId) {
        Map<String,Object> params = new HashMap<>();
        params.put("appId",appId);
        params.put("parentMenuId",parentMenuId);
        return getSqlSession().selectOne("MenuSql.findMenuCount",params);
    }

    @Override
    public Integer findMenuCount(String parentMenuId) {
        return getSqlSession().selectOne("MenuSql.findChildMenuCount",parentMenuId);
    }

    @Override
    public String findParentIdByMenuId(String menuId) {
        return getSqlSession().selectOne("MenuSql.findParentIdByMenuId",menuId);
    }

    @Override
    public List<String> findChildMenusByParentId(String parentId) {
        return getSqlSession().selectList("MenuSql.findChildMenusByParentId",parentId);
    }

    @Override
    public Menu createMenu(Menu menu) {
        getSqlSession().insert("MenuSql.createMenu",menu);
        return menu;
    }

    @Override
    public Integer deleteMenByIds(List<String> menuIds) {
        return getSqlSession().delete("MenuSql.deleteMenByIds",menuIds);
    }

    @Override
    public Integer deleteMensByAppId(String appId) {
        return getSqlSession().delete("MenuSql.deleteMensByAppId",appId);
    }

    @Override
    public Integer setMenuLeaf(String menuId, Boolean isLeaf) {
        Map<String,Object> params = new HashMap<>();
        params.put("menuId",menuId);
        params.put("isLeaf",isLeaf);
        return getSqlSession().update("MenuSql.setMenuLeaf", params);
    }

    @Override
    public Integer correlationApp(String appId,String menuId){
        Map<String,Object> params = new HashMap<>();
        params.put("appId",appId);
        params.put("menuId",menuId);
        return getSqlSession().insert("MenuSql.correlationApp",params);
    }

    @Override
    public Integer uncorrelationAppByMenuIds(List<String> menuIds) {
        return getSqlSession().delete("MenuSql.uncorrelationAppByMenuIds",menuIds);
    }

    @Override
    public List<String> findMenuIdsByRole(String roleId, String appId) {
        Map<String,Object> params = new HashMap<>();
        params.put("roleId",roleId);
        params.put("appId",appId);

        return getSqlSession().selectList("MenuSql.findMenuIdsByRole",params);
    }
}
